package com.majx.controller;

import com.majx.pojo.News;
import com.majx.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/news")
public class NewsController {
    @Autowired
    @Qualifier("NewsService")
    private NewsService newsService;

    /**
     * 查询5条新闻分页数据
     * @param page
     * @return
     */
    @RequestMapping("/queryNewsPage")
    @ResponseBody
    public List<News> queryNewsPage(@RequestParam("page") Integer page) {
        System.out.println("page = "+page);
        Integer pageNow = page == null ? 0 : page;
        Integer pageSize = 5;
        Integer startRow = (pageNow - 1) * pageSize;

        System.out.println("startRow = "+startRow);

        return newsService.queryNewsPage(startRow);
    }

    /**
     * 查询5条新闻分页数据----带条件
     * @param map
     * @return
     */
    @RequestMapping("/selectNewsPage")
    @ResponseBody
    public List<News> selectNewsPage(@RequestBody Map<Object, Object> map) {
        System.out.println("map = "+map);
        String pageStr = map.get("page") == null ? "1" :map.get("page").toString();
        Integer page =Integer.parseInt(pageStr);
        System.out.println("page = "+page);
        Integer pageNow = page;
        Integer pageSize = 5;
        Integer startRow = (pageNow - 1) * pageSize;
        map.put("startRow",startRow);
        map.remove("page");

        return newsService.selectNewsPage(map);
    }

    /**
     * 增加一条新闻记录
     * @param news
     * @return
     */
    @RequestMapping("/addNews")
    @ResponseBody
    public Integer addNews(@RequestBody News news) {
        return newsService.addNews(news);
    }

    /**
     * 更新一条新闻记录
     * @param news
     * @return
     */
    @RequestMapping("/updateNews")
    @ResponseBody
    public Integer updateNews(@RequestBody News news) {
        return newsService.updateNews(news);
    }


    /**
     * 删除一条新闻记录
     * @param nid
     * @return
     */
    @RequestMapping("/deleteNews")
    @ResponseBody
    public Integer deleteNews(@RequestParam("nid") Integer nid) {
        return newsService.deleteNews(nid);
    }

    /**
     * 删除多条新闻记录
     * @param idList
     * @return
     */
    @RequestMapping("/deleteNewsList")
    @ResponseBody
    public Integer deleteNewsList(@RequestParam("idList") String idList) {
        System.out.println("idList = "+idList);
        List<Integer> idArray = new ArrayList<Integer>();
        idList = idList.substring(0,idList.length() - 1);
        for (String item : idList.split(",")) {
            Integer id = Integer.parseInt(item);
            idArray.add(id);
        }

        System.out.println("idArray = "+idArray);

        return newsService.deleteNewsList(idArray);
    }

    /**
     * 获取新闻总数---带条件
     * @param map
     * @return
     */
    @RequestMapping("/getNewsCount")
    @ResponseBody
    public Integer getNewsCount(@RequestBody Map<Object, Object> map) {
        return newsService.getNewsCount(map);
    }
}
